apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: ks-controller-manager
    tier: backend
    version: {{ ks_version }}
  name: ks-controller-manager
  namespace: kubesphere-system
spec:
  strategy:
    rollingUpdate:
      maxSurge: 0
    type: RollingUpdate
  progressDeadlineSeconds: 600
{% if master_num is defined and master_num != "0" %}
  replicas: {{ master_num }}
{% else %}
  replicas: 1
{% endif %}
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: ks-controller-manager
      tier: backend
      version: {{ ks_version }}
  template:
    metadata:
      labels:
        app: ks-controller-manager
        tier: backend
        version: {{ ks_version }}
    spec:
      containers:
      - command:
        - controller-manager
        - --logtostderr=true
        - --leader-elect=true
        image: {{ ks_controller_manager_repo }}:{{ ks_controller_manager_tag }}
        imagePullPolicy: {{ ks_image_pull_policy }}
        name: ks-controller-manager
        ports:
        - containerPort: 8080
          protocol: TCP
        - containerPort: 8443
          protocol: TCP
        resources:
          limits:
            cpu: 1000m
            memory: 1000Mi
          requests:
            cpu: 30m
            memory: 50Mi
        volumeMounts:
        - mountPath: /etc/kubesphere/
          name: kubesphere-config
        - mountPath: /tmp/k8s-webhook-server/serving-certs
          name: webhook-secret
        - mountPath: /var/lib/kubelet/plugins/
          name: kubelet-plugin
        - mountPath: /etc/localtime
          name: host-time
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      serviceAccount: kubesphere
      serviceAccountName: kubesphere
      terminationGracePeriodSeconds: 30
      volumes:
        - name: kubesphere-config
          configMap:
            name: kubesphere-config
            defaultMode: 420
        - name: webhook-secret
          secret:
            defaultMode: 420
            secretName: ks-controller-manager-webhook-cert
        - name: kubelet-plugin
          hostPath:
            path: /var/lib/kubelet/plugins/
            type: DirectoryOrCreate
        - hostPath:
            path: /etc/localtime
            type: ""
          name: host-time
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
        - key: CriticalAddonsOnly
          operator: Exists
        - effect: NoExecute
          key: node.kubernetes.io/not-ready
          operator: Exists
          tolerationSeconds: 60
        - effect: NoExecute
          key: node.kubernetes.io/unreachable
          operator: Exists
          tolerationSeconds: 60
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            preference:
              matchExpressions:
              - key: node-role.kubernetes.io/master
                operator: In
                values:
                - ""
{% if master_num is defined and master_num != "1" %}
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - topologyKey: kubernetes.io/hostname
            labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - ks-controller-manager
            namespaces:
            - kubesphere-system
{% endif %}

---

apiVersion: v1
kind: ServiceAccount
metadata:
  name: ks-controller-manager
  namespace: kubesphere-system

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: ks-controller-manager-role
rules:
  - apiGroups:
      - ""
    resources:
      - namespaces
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - ""
    resources:
      - namespaces/status
    verbs:
      - get
      - update
      - patch
  - apiGroups:
      - tenant.kubesphere.io
    resources:
      - workspaces
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - tenant.kubesphere.io
    resources:
      - workspaces/status
    verbs:
      - get
      - update
      - patch
  - apiGroups:
      - admissionregistration.k8s.io
    resources:
      - mutatingwebhookconfigurations
      - validatingwebhookconfigurations
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - ""
    resources:
      - secrets
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - ""
    resources:
      - services
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - networking.istio.io
    resources:
      - virtualservices
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - networking.istio.io
    resources:
      - destinationrules
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - servicemesh.kubesphere.io
    resources:
      - servicepolicies
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - servicemesh.kubesphere.io
    resources:
      - strategies
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - ""
    resources:
      - deployments
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - ""
    resources:
      - deployments/status
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - rbac.authorization.k8s.io
    resources:
      - clusterrolebindings
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - rbac.authorization.k8s.io
    resources:
      - clusterroles
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
  - apiGroups:
      - app.k8s.io
    resources:
      - apps
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch
      - delete
      
---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: ks-controller-manager-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: ks-controller-manager-role
subjects:
- kind: ServiceAccount
  name: ks-controller-manager
  namespace: kubesphere-system

---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: ks-controller-manager
    tier: backend
    version: v3.0.0
  name: ks-controller-manager
  namespace: kubesphere-system
spec:
  ports:
  - port: 443
    protocol: TCP
    targetPort: 8443
  selector:
    app: ks-controller-manager
    tier: backend
    version: {{ ks_version }}
  sessionAffinity: None
  type: ClusterIP
